package org.aristotle.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aristotle.common.response.ResponseEntity;
import org.aristotle.common.response.result.Result;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.demo.model.entity.Category;
import org.aristotle.demo.service.ICategoryService;
import org.aristotle.demo.model.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("category")
@Api(value="分类controller",tags={"分类操作接口"})
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    /*分页查询*/
    @GetMapping("page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "分类id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "name", paramType = "query", value = "分类名", dataType = "String", required = false),
            @ApiImplicitParam(name = "current", paramType = "query", value = "页码", dataType = "Integer", required = false,defaultValue = "1"),
            @ApiImplicitParam(name = "size", paramType = "query", value = "页距", dataType = "Integer", required = false,defaultValue = "10")
    })
    public Result<Page<CategoryVO>> queryPageList(
            @RequestParam(name = "id",required = false) Long id,
            @RequestParam(name = "name",required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Page<CategoryVO> result = categoryService.queryPageList(id,name,current,size);
        return ResponseEntity.ok(result);
    }

    @GetMapping
    @ApiOperation("根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "分类id", dataType = "Long", required = true)
    })
    public Result<?> queryById(@RequestParam(name = "id") Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        CategoryVO result = categoryService.getById(id);
        if (result == null) {
            return ResponseEntity.notFound();
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("获取列表")
    public Result<?> queryList() {
        return ResponseEntity.ok(AristotleBeanUtils.copyListProperties(categoryService.list(),CategoryVO::new));
    }

    @PostMapping
    @ApiOperation("添加")
    public Result<?> add(@RequestBody CategoryVO categoryVO) {
        Category category = AristotleBeanUtils.transitionBean(categoryVO, Category::new);
        category.setId(null);
        categoryService.save(category);
        return ResponseEntity.addOk(categoryVO);
    }

    @PutMapping
    @ApiOperation("修改")
    public Result<?> edit(@RequestBody CategoryVO categoryVO) {
        if (categoryVO.getId() == null) {
            return ResponseEntity.badRequest();
        }
        Category category = AristotleBeanUtils.transitionBean(categoryVO, Category::new);
        categoryService.updateById(category);
        return ResponseEntity.updateOk(categoryVO);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", value = "分类id", dataType = "Long", required = true)
    })
    public Result<?> delete(@PathVariable Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        categoryService.removeById(id);
        return ResponseEntity.deleteOk(id);
    }

}
